# 手动添加测试数据
import datetime
import random

from info import db
from info.models import User
from manage import app


def add_test_users():
    """添加测试数据"""
    users  = list()
    now = datetime.datetime.now()  # 当前时间

    for num in range(0, 10000):
        try:
            user = User()  # 用户模型

            user.nick_name = '%011d' % num
            user.mobile = "%011d" % num
            user.password_hash = "pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829"
            # 最后一次登陆时间, 当前时间减去一个0到31天之间的随机时间
            user.last_login = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
        except Exception as e:
            print(e)

        with app.app_context():
            db.session.add_all(users)
            db.session.commit()


if __name__ == '__main__':
    # add_test_users()
    print('ok')